import { createRouter, createWebHashHistory, type RouteRecordRaw } from 'vue-router'
import Login from '@/views/login/LoginPage.vue'
import Layout from '@/layout/MyLayout.vue'
import Home from '@/views/home/HomePage.vue'
import NotFound from '@/views/errors/NotFoundPage.vue'

// 动态路由，用于匹配菜单动态添加路由
const asyncRoutes: Array<RouteRecordRaw> = [
  {
    path: '/home',
    component: Home,
    meta: {
      title: '首页'
    }
  },
  {
    path: '/admin',
    redirect: '/admin/user',
    children: [
      {
        path: '/admin/user',
        component: () => import('@/views/admin/UserPage.vue'),
        meta: {
          title: '用户管理'
        }
      },
      {
        path: '/admin/role',
        component: () => import('@/views/admin/RolePage.vue'),
        meta: {
          title: '角色管理'
        }
      },
      {
        path: '/admin/authority',
        component: () => import('@/views/admin/AuthorityPage.vue'),
        meta: {
          title: '权限管理'
        }
      }
    ],
    meta: {
      title: '系统管理'
    }
  }
]
// 默认路由 所有用户共享
const publicRoutes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/home',
    component: Layout,
    // 子路由
    children: asyncRoutes
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/:pathMatch(.*)*',
    component: NotFound,
    name: 'NotFound'
  }
]
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: publicRoutes
})

export default router
